﻿using DLL;
using gregn6Lib;
using NursingFormSystem.Common;
using NursingFormSystem.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Linq;

namespace NursingFormSystem.NursingRecordsIntegration
{
    public partial class NursingRecordsIntegrationPrints : Form
    {
        private GridppReport Report = new GridppReport();
        NurFormsService.NursingForm formService = new NurFormsService.NursingForm();
        PatientInfo PatientInfo;
        string Id;
        public NursingRecordsIntegrationPrints(Tb_User loginUser, PatientInfo patientInfo, string id)
        {
            InitializeComponent();
            PatientInfo = patientInfo;
            this.Id = id;
            Report.LoadFromFile(System.Windows.Forms.Application.StartupPath + @"\GRFModel\Ydhl_GRF\PICU护理记录整合.grf");
            Report.ConnectionString = Utility.GetDatabaseConnectionString();
            Report.FetchRecord += new _IGridppReportEvents_FetchRecordEventHandler(ReportFetchRecord);
            axGRPrintViewer1.Report = Report;
        }

        private void ReportFetchRecord()
        {
            var ret = formService.GetNursingFormRecord(PatientInfo.PATIENT_ID, PatientInfo.VISIT_ID, (int)FormType.NursingRecordsIntegration);
            DataTable dt = new DataTable();
            if (ret.Code == 1)
            {
                CreateDataTableHead(dt);
                List<NursingFormRecord> list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<NursingFormRecord>>(ret.D);
                if (!string.IsNullOrEmpty(Id))
                    list = list.Where(p => p.Id == Convert.ToInt32(Id)).ToList() ;
                for (int i = 0; i < list.Count; i++)
                {
                    NursingFormRecord record1 = list[i];
                    Model.NursingRecordsIntegration ps = XmlUtil.XMLToObject<Model.NursingRecordsIntegration>(record1.NursingFormData, Encoding.UTF8);
                    dt.Rows.Add(
                        PatientInfo.DEPARTMENT_NAME,//DEPARTMENT_NAME
                        PatientInfo.NURSE_CELL_NAME,//NURSE_CELL_NAME
                        PatientInfo.BED_NO,//BED_NO
                        record1.PatientName,
                        PatientInfo.SEX,//SEX
                        PatientInfo.AGE,//AGE
                        PatientInfo.PATIENT_ID,//PATIENTID
                        Convert.ToDateTime(PatientInfo.IN_DEPARTMENT_DATE).ToString("yyyy-MM-dd"),//入院日期
                        //生命体征
                        ps.ShengMingTiZheng.T.Split('#')[0],
                        ps.ShengMingTiZheng.T.Split('#')[1],
                        ps.ShengMingTiZheng.T.Split('#')[2],
                        ps.ShengMingTiZheng.T.Split('#')[3],
                        ps.ShengMingTiZheng.T.Split('#')[4],
                        ps.ShengMingTiZheng.T.Split('#')[5],
                        ps.ShengMingTiZheng.T.Split('#')[6],
                        ps.ShengMingTiZheng.T.Split('#')[7],
                        ps.ShengMingTiZheng.T.Split('#')[8],
                        ps.ShengMingTiZheng.T.Split('#')[9],
                        ps.ShengMingTiZheng.T.Split('#')[10],
                        ps.ShengMingTiZheng.T.Split('#')[11],

                        ps.ShengMingTiZheng.HR.Split('#')[0],
                        ps.ShengMingTiZheng.HR.Split('#')[1],
                        ps.ShengMingTiZheng.HR.Split('#')[2],
                        ps.ShengMingTiZheng.HR.Split('#')[3],
                        ps.ShengMingTiZheng.HR.Split('#')[4],
                        ps.ShengMingTiZheng.HR.Split('#')[5],
                        ps.ShengMingTiZheng.HR.Split('#')[6],
                        ps.ShengMingTiZheng.HR.Split('#')[7],
                        ps.ShengMingTiZheng.HR.Split('#')[8],
                        ps.ShengMingTiZheng.HR.Split('#')[9],
                        ps.ShengMingTiZheng.HR.Split('#')[10],
                        ps.ShengMingTiZheng.HR.Split('#')[11],

                        ps.ShengMingTiZheng.R.Split('#')[0],
                        ps.ShengMingTiZheng.R.Split('#')[1],
                        ps.ShengMingTiZheng.R.Split('#')[2],
                        ps.ShengMingTiZheng.R.Split('#')[3],
                        ps.ShengMingTiZheng.R.Split('#')[4],
                        ps.ShengMingTiZheng.R.Split('#')[5],
                        ps.ShengMingTiZheng.R.Split('#')[6],
                        ps.ShengMingTiZheng.R.Split('#')[7],
                        ps.ShengMingTiZheng.R.Split('#')[8],
                        ps.ShengMingTiZheng.R.Split('#')[9],
                        ps.ShengMingTiZheng.R.Split('#')[10],
                        ps.ShengMingTiZheng.R.Split('#')[11],

                        ps.ShengMingTiZheng.BP.Split('#')[0],
                        ps.ShengMingTiZheng.BP.Split('#')[1],
                        ps.ShengMingTiZheng.BP.Split('#')[2],
                        ps.ShengMingTiZheng.BP.Split('#')[3],
                        ps.ShengMingTiZheng.BP.Split('#')[4],
                        ps.ShengMingTiZheng.BP.Split('#')[5],
                        ps.ShengMingTiZheng.BP.Split('#')[6],
                        ps.ShengMingTiZheng.BP.Split('#')[7],
                        ps.ShengMingTiZheng.BP.Split('#')[8],
                        ps.ShengMingTiZheng.BP.Split('#')[9],
                        ps.ShengMingTiZheng.BP.Split('#')[10],
                        ps.ShengMingTiZheng.BP.Split('#')[11],

                        ps.ShengMingTiZheng.SPO2.Split('#')[0],
                        ps.ShengMingTiZheng.SPO2.Split('#')[1],
                        ps.ShengMingTiZheng.SPO2.Split('#')[2],
                        ps.ShengMingTiZheng.SPO2.Split('#')[3],
                        ps.ShengMingTiZheng.SPO2.Split('#')[4],
                        ps.ShengMingTiZheng.SPO2.Split('#')[5],
                        ps.ShengMingTiZheng.SPO2.Split('#')[6],
                        ps.ShengMingTiZheng.SPO2.Split('#')[7],
                        ps.ShengMingTiZheng.SPO2.Split('#')[8],
                        ps.ShengMingTiZheng.SPO2.Split('#')[9],
                        ps.ShengMingTiZheng.SPO2.Split('#')[10],
                        ps.ShengMingTiZheng.SPO2.Split('#')[11],
                        //意识
                        ps.YiShi.YiShiZhuangTai.Split('#')[0],
                        ps.YiShi.YiShiZhuangTai.Split('#')[1],
                        ps.YiShi.YiShiZhuangTai.Split('#')[2],
                        ps.YiShi.YiShiZhuangTai.Split('#')[3],
                        ps.YiShi.YiShiZhuangTai.Split('#')[4],
                        ps.YiShi.YiShiZhuangTai.Split('#')[5],
                        ps.YiShi.YiShiZhuangTai.Split('#')[6],
                        ps.YiShi.YiShiZhuangTai.Split('#')[7],
                        ps.YiShi.YiShiZhuangTai.Split('#')[8],
                        ps.YiShi.YiShiZhuangTai.Split('#')[9],
                        ps.YiShi.YiShiZhuangTai.Split('#')[10],
                        ps.YiShi.YiShiZhuangTai.Split('#')[11],
                        //护理
                        ps.HuLi.WuHua.Split('#')[0],
                        ps.HuLi.WuHua.Split('#')[1],
                        ps.HuLi.WuHua.Split('#')[2],
                        ps.HuLi.WuHua.Split('#')[3],
                        ps.HuLi.WuHua.Split('#')[4],
                        ps.HuLi.WuHua.Split('#')[5],
                        ps.HuLi.WuHua.Split('#')[6],
                        ps.HuLi.WuHua.Split('#')[7],
                        ps.HuLi.WuHua.Split('#')[8],
                        ps.HuLi.WuHua.Split('#')[9],
                        ps.HuLi.WuHua.Split('#')[10],
                        ps.HuLi.WuHua.Split('#')[11],

                        ps.HuLi.JiChuHuLi.Split('#')[0],
                        ps.HuLi.JiChuHuLi.Split('#')[1],
                        ps.HuLi.JiChuHuLi.Split('#')[2],
                        ps.HuLi.JiChuHuLi.Split('#')[3],
                        ps.HuLi.JiChuHuLi.Split('#')[4],
                        ps.HuLi.JiChuHuLi.Split('#')[5],
                        ps.HuLi.JiChuHuLi.Split('#')[6],
                        ps.HuLi.JiChuHuLi.Split('#')[7],
                        ps.HuLi.JiChuHuLi.Split('#')[8],
                        ps.HuLi.JiChuHuLi.Split('#')[9],
                        ps.HuLi.JiChuHuLi.Split('#')[10],
                        ps.HuLi.JiChuHuLi.Split('#')[11],
                        //出量
                        ps.ChuLiang.NiaoLiang.Split('#')[0],
                        ps.ChuLiang.NiaoLiang.Split('#')[1],
                        ps.ChuLiang.NiaoLiang.Split('#')[2],
                        ps.ChuLiang.NiaoLiang.Split('#')[3],
                        ps.ChuLiang.NiaoLiang.Split('#')[4],
                        ps.ChuLiang.NiaoLiang.Split('#')[5],
                        ps.ChuLiang.NiaoLiang.Split('#')[6],
                        ps.ChuLiang.NiaoLiang.Split('#')[7],
                        ps.ChuLiang.NiaoLiang.Split('#')[8],
                        ps.ChuLiang.NiaoLiang.Split('#')[9],
                        ps.ChuLiang.NiaoLiang.Split('#')[10],
                        ps.ChuLiang.NiaoLiang.Split('#')[11],

                        ps.ChuLiang.QiTa.Split('#')[0],
                        ps.ChuLiang.QiTa.Split('#')[1],
                        ps.ChuLiang.QiTa.Split('#')[2],
                        ps.ChuLiang.QiTa.Split('#')[3],
                        ps.ChuLiang.QiTa.Split('#')[4],
                        ps.ChuLiang.QiTa.Split('#')[5],
                        ps.ChuLiang.QiTa.Split('#')[6],
                        ps.ChuLiang.QiTa.Split('#')[7],
                        ps.ChuLiang.QiTa.Split('#')[8],
                        ps.ChuLiang.QiTa.Split('#')[9],
                        ps.ChuLiang.QiTa.Split('#')[10],
                        ps.ChuLiang.QiTa.Split('#')[11],
                        ps.ChuLiang.QiTa.Split('#')[12],
                        ps.ChuLiang.QiTa.Split('#')[13],
                        ps.ChuLiang.QiTa.Split('#')[14],
                        ps.ChuLiang.QiTa.Split('#')[15],
                        ps.ChuLiang.QiTa.Split('#')[16],
                        ps.ChuLiang.QiTa.Split('#')[17],
                        ps.ChuLiang.QiTa.Split('#')[18],
                        ps.ChuLiang.QiTa.Split('#')[19],
                        ps.ChuLiang.QiTa.Split('#')[20],
                        ps.ChuLiang.QiTa.Split('#')[21],
                        ps.ChuLiang.QiTa.Split('#')[22],
                        ps.ChuLiang.QiTa.Split('#')[23],

                        ps.BingQingJiCuoShi,
                        "",
                        "",
                        ps.ShiErXiaoShiChuRuLiang.ZongRuLiang,
                        ps.ShiErXiaoShiChuRuLiang.WeiChangRuLiang,
                        ps.ShiErXiaoShiChuRuLiang.JingMaiRuLiang,
                        ps.ShiErXiaoShiChuRuLiang.ZongRuLiangQiTa,
                        ps.ShiErXiaoShiChuRuLiang.ZongChuLiang,
                        ps.ShiErXiaoShiChuRuLiang.NiaoLiang,
                        ps.ShiErXiaoShiChuRuLiang.DaBian,
                        ps.ShiErXiaoShiChuRuLiang.NiaoFenJieHe,
                        ps.ShiErXiaoShiChuRuLiang.ZongChuLiangQiTa,
                        ps.HuShiQianMing,
                        ps.HuShiQianMingShangYe,
                        ps.HuShiQianMingXiaYe,

                        ps.RiQi
                        );
                }
                Utility.FillRecordToReport(Report, dt);
            }
            else if (ret.Code == 2)
            {
                CreateDataTableHead(dt);
                DataRow dr = dt.NewRow();
                dt.Rows.Add(dr);
                Utility.FillRecordToReport(Report, dt);
            }
            else if (ret.Code == 0)
            {
                MessageBox.Show(ret.M);
            }
        }
        /// <summary>
        /// 加载列头
        /// </summary>
        /// <param name="dt"></param>
        private static void CreateDataTableHead(DataTable dt)
        {
            dt.Columns.Add("DEPARTMENT_NAME", typeof(string));
            dt.Columns.Add("NURSE_CELL_NAME", typeof(string));
            dt.Columns.Add("BED_NO", typeof(string));
            dt.Columns.Add("PatientName", typeof(string));
            dt.Columns.Add("SEX", typeof(string));
            dt.Columns.Add("AGE", typeof(string));
            dt.Columns.Add("PatientId", typeof(string));
            dt.Columns.Add("IN_DEPARTMENT_DATE", typeof(string));

            dt.Columns.Add("shengmingtizhengT1", typeof(string));
            dt.Columns.Add("shengmingtizhengT2", typeof(string));
            dt.Columns.Add("shengmingtizhengT3", typeof(string));
            dt.Columns.Add("shengmingtizhengT4", typeof(string));
            dt.Columns.Add("shengmingtizhengT5", typeof(string));
            dt.Columns.Add("shengmingtizhengT6", typeof(string));
            dt.Columns.Add("shengmingtizhengT7", typeof(string));
            dt.Columns.Add("shengmingtizhengT8", typeof(string));
            dt.Columns.Add("shengmingtizhengT9", typeof(string));
            dt.Columns.Add("shengmingtizhengT10", typeof(string));
            dt.Columns.Add("shengmingtizhengT11", typeof(string));
            dt.Columns.Add("shengmingtizhengT12", typeof(string));
            dt.Columns.Add("shengmingtizhengHR1", typeof(string));
            dt.Columns.Add("shengmingtizhengHR2", typeof(string));
            dt.Columns.Add("shengmingtizhengHR3", typeof(string));
            dt.Columns.Add("shengmingtizhengHR4", typeof(string));
            dt.Columns.Add("shengmingtizhengHR5", typeof(string));
            dt.Columns.Add("shengmingtizhengHR6", typeof(string));
            dt.Columns.Add("shengmingtizhengHR7", typeof(string));
            dt.Columns.Add("shengmingtizhengHR8", typeof(string));
            dt.Columns.Add("shengmingtizhengHR9", typeof(string));
            dt.Columns.Add("shengmingtizhengHR10", typeof(string));
            dt.Columns.Add("shengmingtizhengHR11", typeof(string));
            dt.Columns.Add("shengmingtizhengHR12", typeof(string));
            dt.Columns.Add("shengmingtizhengR1", typeof(string));
            dt.Columns.Add("shengmingtizhengR2", typeof(string));
            dt.Columns.Add("shengmingtizhengR3", typeof(string));
            dt.Columns.Add("shengmingtizhengR4", typeof(string));
            dt.Columns.Add("shengmingtizhengR5", typeof(string));
            dt.Columns.Add("shengmingtizhengR6", typeof(string));
            dt.Columns.Add("shengmingtizhengR7", typeof(string));
            dt.Columns.Add("shengmingtizhengR8", typeof(string));
            dt.Columns.Add("shengmingtizhengR9", typeof(string));
            dt.Columns.Add("shengmingtizhengR10", typeof(string));
            dt.Columns.Add("shengmingtizhengR11", typeof(string));
            dt.Columns.Add("shengmingtizhengR12", typeof(string));
            dt.Columns.Add("shengmingtizhengBP1", typeof(string));
            dt.Columns.Add("shengmingtizhengBP2", typeof(string));
            dt.Columns.Add("shengmingtizhengBP3", typeof(string));
            dt.Columns.Add("shengmingtizhengBP4", typeof(string));
            dt.Columns.Add("shengmingtizhengBP5", typeof(string));
            dt.Columns.Add("shengmingtizhengBP6", typeof(string));
            dt.Columns.Add("shengmingtizhengBP7", typeof(string));
            dt.Columns.Add("shengmingtizhengBP8", typeof(string));
            dt.Columns.Add("shengmingtizhengBP9", typeof(string));
            dt.Columns.Add("shengmingtizhengBP10", typeof(string));
            dt.Columns.Add("shengmingtizhengBP11", typeof(string));
            dt.Columns.Add("shengmingtizhengBP12", typeof(string));
            dt.Columns.Add("shengmingtizhengSP1", typeof(string));
            dt.Columns.Add("shengmingtizhengSP2", typeof(string));
            dt.Columns.Add("shengmingtizhengSP3", typeof(string));
            dt.Columns.Add("shengmingtizhengSP4", typeof(string));
            dt.Columns.Add("shengmingtizhengSP5", typeof(string));
            dt.Columns.Add("shengmingtizhengSP6", typeof(string));
            dt.Columns.Add("shengmingtizhengSP7", typeof(string));
            dt.Columns.Add("shengmingtizhengSP8", typeof(string));
            dt.Columns.Add("shengmingtizhengSP9", typeof(string));
            dt.Columns.Add("shengmingtizhengSP10", typeof(string));
            dt.Columns.Add("shengmingtizhengSP11", typeof(string));
            dt.Columns.Add("shengmingtizhengSP12", typeof(string));
            dt.Columns.Add("yishizhuangtai1", typeof(string));
            dt.Columns.Add("yishizhuangtai2", typeof(string));
            dt.Columns.Add("yishizhuangtai3", typeof(string));
            dt.Columns.Add("yishizhuangtai4", typeof(string));
            dt.Columns.Add("yishizhuangtai5", typeof(string));
            dt.Columns.Add("yishizhuangtai6", typeof(string));
            dt.Columns.Add("yishizhuangtai7", typeof(string));
            dt.Columns.Add("yishizhuangtai8", typeof(string));
            dt.Columns.Add("yishizhuangtai9", typeof(string));
            dt.Columns.Add("yishizhuangtai10", typeof(string));
            dt.Columns.Add("yishizhuangtai11", typeof(string));
            dt.Columns.Add("yishizhuangtai12", typeof(string));
            dt.Columns.Add("wuhua1", typeof(string));
            dt.Columns.Add("wuhua2", typeof(string));
            dt.Columns.Add("wuhua3", typeof(string));
            dt.Columns.Add("wuhua4", typeof(string));
            dt.Columns.Add("wuhua5", typeof(string));
            dt.Columns.Add("wuhua6", typeof(string));
            dt.Columns.Add("wuhua7", typeof(string));
            dt.Columns.Add("wuhua8", typeof(string));
            dt.Columns.Add("wuhua9", typeof(string));
            dt.Columns.Add("wuhua10", typeof(string));
            dt.Columns.Add("wuhua11", typeof(string));
            dt.Columns.Add("wuhua12", typeof(string));
            dt.Columns.Add("jichuhuli1", typeof(string));
            dt.Columns.Add("jichuhuli2", typeof(string));
            dt.Columns.Add("jichuhuli3", typeof(string));
            dt.Columns.Add("jichuhuli4", typeof(string));
            dt.Columns.Add("jichuhuli5", typeof(string));
            dt.Columns.Add("jichuhuli6", typeof(string));
            dt.Columns.Add("jichuhuli7", typeof(string));
            dt.Columns.Add("jichuhuli8", typeof(string));
            dt.Columns.Add("jichuhuli9", typeof(string));
            dt.Columns.Add("jichuhuli10", typeof(string));
            dt.Columns.Add("jichuhuli11", typeof(string));
            dt.Columns.Add("jichuhuli12", typeof(string));
            dt.Columns.Add("niaoliang1", typeof(string));
            dt.Columns.Add("niaoliang2", typeof(string));
            dt.Columns.Add("niaoliang3", typeof(string));
            dt.Columns.Add("niaoliang4", typeof(string));
            dt.Columns.Add("niaoliang5", typeof(string));
            dt.Columns.Add("niaoliang6", typeof(string));
            dt.Columns.Add("niaoliang7", typeof(string));
            dt.Columns.Add("niaoliang8", typeof(string));
            dt.Columns.Add("niaoliang9", typeof(string));
            dt.Columns.Add("niaoliang10", typeof(string));
            dt.Columns.Add("niaoliang11", typeof(string));
            dt.Columns.Add("niaoliang12", typeof(string));
            dt.Columns.Add("chuliangqita1", typeof(string));
            dt.Columns.Add("chuliangqita2", typeof(string));
            dt.Columns.Add("chuliangqita3", typeof(string));
            dt.Columns.Add("chuliangqita4", typeof(string));
            dt.Columns.Add("chuliangqita5", typeof(string));
            dt.Columns.Add("chuliangqita6", typeof(string));
            dt.Columns.Add("chuliangqita7", typeof(string));
            dt.Columns.Add("chuliangqita8", typeof(string));
            dt.Columns.Add("chuliangqita9", typeof(string));
            dt.Columns.Add("chuliangqita10", typeof(string));
            dt.Columns.Add("chuliangqita11", typeof(string));
            dt.Columns.Add("chuliangqita12", typeof(string));
            dt.Columns.Add("chuliangqita13", typeof(string));
            dt.Columns.Add("chuliangqita14", typeof(string));
            dt.Columns.Add("chuliangqita15", typeof(string));
            dt.Columns.Add("chuliangqita16", typeof(string));
            dt.Columns.Add("chuliangqita17", typeof(string));
            dt.Columns.Add("chuliangqita18", typeof(string));
            dt.Columns.Add("chuliangqita19", typeof(string));
            dt.Columns.Add("chuliangqita20", typeof(string));
            dt.Columns.Add("chuliangqita21", typeof(string));
            dt.Columns.Add("chuliangqita22", typeof(string));
            dt.Columns.Add("chuliangqita23", typeof(string));
            dt.Columns.Add("chuliangqita24", typeof(string));
            dt.Columns.Add("bingqingjicuoshi1", typeof(string));
            dt.Columns.Add("bingqingjicuoshi2", typeof(string));
            dt.Columns.Add("bingqingjicuoshi3", typeof(string));
            dt.Columns.Add("zongruliang", typeof(string));
            dt.Columns.Add("weichangluliang", typeof(string));
            dt.Columns.Add("jingmailuliang", typeof(string));
            dt.Columns.Add("zongruliangqita", typeof(string));
            dt.Columns.Add("zongchuliang", typeof(string));
            dt.Columns.Add("niaoliang", typeof(string));
            dt.Columns.Add("dabian", typeof(string));
            dt.Columns.Add("niaofenhunhe", typeof(string));
            dt.Columns.Add("zongchuliangqita", typeof(string));
            dt.Columns.Add("hushiqianming1", typeof(string));
            dt.Columns.Add("hushiqianming2", typeof(string));
            dt.Columns.Add("hushiqianming3", typeof(string));
            dt.Columns.Add("RiQi", typeof(string));
        }
        /// <summary>
        /// 检查数据项目
        /// </summary>
        /// <param name="str"></param>
        /// <param name="len"></param>
        /// <returns></returns>
        private bool ChkStr(string str, int len)
        {
            if (string.IsNullOrEmpty(str)) return false;
            if (str.IndexOf("#") < 0) return false;
            if (str.Split('#').Length < len) return false;
            return true;
        }
        private void NursingRecordsIntegrationPrints_Load(object sender, EventArgs e)
        {
            axGRPrintViewer1.Start();
        }
    }
}
